package com.xsy.android.compose.we.component.ui.page.chat.compose

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.spring
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.xsy.android.compose.we.component.R
import com.xsy.android.compose.we.component.data.Msg
import com.xsy.android.compose.we.component.data.User
import com.xsy.android.compose.we.component.ui.theme.WeComposeTheme
import kotlinx.coroutines.delay

/**
 *@author: xiaosy
 *时间: 2024/7/3
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun MessageItem(msg: Msg, shakingTime: Int = 0, shakingLevel: Int = 0) {
    val shakingAngleBubble = remember {
        Animatable(0f)
    }
    LaunchedEffect(key1 = shakingTime) {
        if (shakingTime != 0) {
            delay(shakingLevel.toLong() * 30)
            shakingAngleBubble.animateTo(
                0f,
                animationSpec = spring(0.4f, 500f),
                initialVelocity = 1200f / (1 + shakingLevel * 0.4f)
            )
        }
    }
    if (msg.from == User.Me) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp),
            horizontalArrangement = Arrangement.End

        ) {
            val bubbleColor = WeComposeTheme.colors.bubbleMe
            Text(
                text = msg.text,
                modifier = Modifier
                    .graphicsLayer {
                        rotationZ = shakingAngleBubble.value
                        transformOrigin = TransformOrigin(1f, 0f)
                    }
                    .drawBehind {
                        val bubble = Path().apply {
                            val rect = RoundRect(
                                10.dp.toPx(),
                                0f,
                                size.width - 10.dp.toPx(),
                                size.height,
                                4.dp.toPx(),
                                4.dp.toPx()
                            )
                            addRoundRect(rect)
                            moveTo(size.width - 10.dp.toPx(), 15.dp.toPx())
                            lineTo(size.width - 5.dp.toPx(), 20.dp.toPx())
                            lineTo(size.width - 10.dp.toPx(), 25.dp.toPx())
                            close()
                        }
                        drawPath(bubble, bubbleColor)
                    }
                    .padding(20.dp, 10.dp),
                color = WeComposeTheme.colors.textPrimaryMe
            )
            Image(
                painter = painterResource(id = msg.from.avatar),
                contentDescription = msg.from.name,
                modifier = Modifier
                    .graphicsLayer(
                        rotationZ = shakingAngleBubble.value * 0.6f,
                        transformOrigin = TransformOrigin(1f, 0f)
                    )
                    .size(40.dp)
                    .clip(
                        RoundedCornerShape(4.dp)
                    )
            )
        }
    } else {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
        ) {
            Image(
                painter = painterResource(id = msg.from.avatar),
                contentDescription = msg.from.name,
                modifier = Modifier
                    .graphicsLayer(
                        rotationZ = shakingAngleBubble.value * 0.6f,
                        transformOrigin = TransformOrigin(0f, 0f)
                    )
                    .size(40.dp)
                    .clip(
                        RoundedCornerShape(4.dp)
                    )
            )
            val bubbleColor = WeComposeTheme.colors.bubbleOthers
            Text(
                text = msg.text,
                modifier = Modifier
                    .graphicsLayer {
                        rotationZ = -shakingAngleBubble.value
                        transformOrigin = TransformOrigin(0f, 0f)
                    }
                    .drawBehind {
                        val bubble = Path().apply {
                            val rect = RoundRect(
                                10.dp.toPx(),
                                0f,
                                size.width - 10.dp.toPx(),
                                size.height,
                                4.dp.toPx(),
                                4.dp.toPx()
                            )
                            addRoundRect(rect)
                            moveTo(10.dp.toPx(), 15.dp.toPx())
                            lineTo(5.dp.toPx(), 20.dp.toPx())
                            lineTo(10.dp.toPx(), 25.dp.toPx())
                            close()
                        }
                        drawPath(bubble, bubbleColor)
                    }
                    .padding(20.dp, 10.dp),
                color = WeComposeTheme.colors.textPrimary
            )
        }
    }
}


@Preview(showBackground = true)
@Composable
fun PreviewMessageItem1() {
    MessageItem(msg = Msg(User.Me, "哈哈昂", "13:48"))
}


@Preview(showBackground = true)
@Composable
fun PreviewMessageItem2() {
    val msg = Msg(
        User("diuwuxian", "丢物线", R.drawable.avatar_diuwuxian),
        "你笑个屁呀",
        "13:48"
    )
    MessageItem(msg)
}